#include "TpcServer.h"
#include <string.h>
#include <iostream>
#include <messSocket.h>
#include "Message.h"
#include <unistd.h>
#include <sys/epoll.h>
#include "MessageDto.h"
#include <glib.h>

using namespace std;

void* fn(void* param){
    MessageDto* dto=(MessageDto*)param;
    messSocket *socket=dto->socket;

    //收消息
    Message* msg=socket->receiveMag(3000);
    int len=msg->length;
    if (len==-1)
    {
        cout<<"读取失败"<<endl;
    }else if (len==0)
    {
        cout<<"客户端关闭"<<endl;
        epoll_ctl(dto->epollId,EPOLL_CTL_DEL,dto->connFd,NULL);
        close(dto->connFd);
    }else{
        int size=socket->sendMag(msg,3000);
        cout<<size<<endl;
    }
    delete dto;
    return nullptr;
}

void func(gpointer data,gpointer user_data){
    MessageDto* dto=(MessageDto*)data;
    messSocket* socket=dto->socket;

    Message *msg=socket->receiveMag(3000);
    cout<<msg->content<<endl;
    int len=msg->length;
    if (len==-1)
    {
        cout<<"读取失败"<<endl;
    }else if (len==0)
    {
        cout<<"客户端关闭"<<endl;
        epoll_ctl(dto->epollId,EPOLL_CTL_DEL,socket->getConnFd(),NULL);
        close(socket->getConnFd());
    }else{
        int size=socket->sendMag(msg,3000);
        cout<<"发送成功,"<<size<<endl;
    }
    delete dto;
}

int main(int argc, char const *argv[])
{
    TpcServer server(8080);
    server.startWithPool(func);
    return 0;
}
